package com.beordie.wx;

import com.aliyuncs.utils.StringUtils;
import com.beordie.model.Message;
import com.beordie.model.Users;
import com.beordie.mvc.ResponseText;
import com.beordie.mvc.ResponseView;
import com.beordie.service.IUserService;
import com.beordie.service.impl.UserServiceImpl;
import com.beordie.utils.GenerationCode;
import com.beordie.utils.JsonUtils;
import com.beordie.utils.UserUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Classname UserController
 * @Description 用户
 * @Date 2021/6/30 22:27
 * @Created 30500
 */
public class UserController {
    IUserService service = new UserServiceImpl();

    /**
     * @description 获取验证码
     * @author 30500
     * @date 2021/7/1 7:46
     * @type [javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse]
     * @return java.lang.String
     */
    @ResponseText("/wx/cellCode.udo")
    public String getCode(HttpServletRequest request, HttpServletResponse response) {
        // 获取临时手机号的登陆验证码
        String userPhone = request.getParameter("userPhone");
        String cellCode = GenerationCode.generationCode();

        UserUtils.setTempInfo(request.getSession(), userPhone, cellCode);
        // 前端信息
        Message message = new Message();
        message.setResult("验证码: " + cellCode);
        message.setStatus(0);
        String json = JsonUtils.parseObject(message);
        return json;
    }

    /**
     * @description 更新信息时获取验证码
     * @author 30500
     * @date 2021/10/2 16:23
     * @type [javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse]
     * @return java.lang.String
     */
    @ResponseText("/wx/updateCellCode.udo")
    public String getPhoneCode(HttpServletRequest request, HttpServletResponse response) {
        String userPhone = UserUtils.getUserInfo(request.getSession()).getUserPhone();
        String phone = request.getParameter("userPhone");
        // 手机号匹配发送验证码
        if (userPhone.equals(phone)) {
            String code = getCode(request, response);
            return code;
        } else {
            return JsonUtils.parseObject(new Message(-1, "前后手机号不匹配"));
        }
    }

    /**
     * @description 根据手机号和密码进行登录
     * @author 30500
     * @date 2021/10/23 8:07
     * @type [javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse]
     * @return java.lang.String
     */
    @ResponseText("/wx/loginP.udo")
    public String loginPassword(HttpServletRequest request, HttpServletResponse response) {
        String phone = request.getParameter("phone");
        String password = request.getParameter("code");
        Users user = service.getByNamePass(phone, password);
        Message message = new Message();
        if (user != null) {  // 验证通过
            Integer identity = user.getIdentity();
            if (identity == 0) { // 管理员账号
                message.setStatus(0);
                message.setResult("快递员登陆成功");
                UserUtils.setAuthority(request.getSession(), true);
            } else { // 普通用户账号登录
                message.setStatus(1);
                message.setResult("用户登陆成功");
                UserUtils.setAuthority(request.getSession(), false);
            }
            // 返回结果
            message.setData(user);
            UserUtils.setUserInfo(request.getSession(), user);
        } else { // 密码或者是手机号验证失败
            message.setStatus(-1);
            message.setResult("密码或者手机号错误");
        }
        return JsonUtils.parseObject(message);
    }
    /**
     * @description 手机号 登陆 / 注册
     * @author 30500
     * @date 2021/7/1 7:47
     * @type [javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse]
     * @return java.lang.String
     */
    @ResponseText("/wx/login.udo")
    public String login(HttpServletRequest request, HttpServletResponse response) {
        String phone = request.getParameter("phone");
        String code = request.getParameter("code");
        String cellCode = UserUtils.getTempInfo(request.getSession(), phone);
        Message message = new Message();
        Users users = null;
        // 检查验证码是否正确
        if (cellCode != null) { // 该手机号获取验证码
            if (!Objects.equals(code, cellCode)) { // 验证码错误
                message.setStatus(-1);
                message.setResult("验证码错误");
            } else { // 验证码正确
                users = service.getByPhone(phone, 0);
                if (users == null) { // 不是管理员
                    users = service.getByPhone(phone, 1);
                    if (users == null) { // 不是用户 , 默认注册用户
                        service.insert("佚名", "123456", phone, "522424199912129999", 1);
                        users = service.getByPhone(phone, 1);
                        message.setStatus(1);
                        message.setResult("用户注册成功");
                    } else { // 用户手机号
                        message.setStatus(1);
                        message.setResult("用户登陆成功");
                    }
                    UserUtils.setAuthority(request.getSession(), false);
                } else { // 管理员手机号
                    message.setStatus(0);
                    message.setResult("快递员登陆成功");
                    UserUtils.setAuthority(request.getSession(), true);
                }
                // 到这一步 users 肯定有数据
                UserUtils.setUserInfo(request.getSession(), users);
                service.updateIpLoginTime(users.getId(), request.getRemoteAddr(), new Date());
            }
        } else { // 该手机未获取验证码
            message.setStatus(-1);
            message.setResult("该手机未获取验证码");
        }
        // 返回结果
        message.setData(users);
        String json = JsonUtils.parseObject(message);
        return json;
    }

    /**
     * @description 退出
     * @author 30500
     * @date 2021/7/1 8:30
     * @type [javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse]
     * @return java.lang.String
     */
    @ResponseText("/wx/logout.udo")
    public String quit(HttpServletRequest request, HttpServletResponse response) {
        UserUtils.logout(request.getSession());
        Message message = new Message(0, "退出登陆");
        return JsonUtils.parseObject(message);
    }

    /**
     * @description 查询用户权限
     * @author 30500
     * @date 2021/7/1 16:26
     * @type [javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse]
     * @return java.lang.String
     */
    @ResponseText("/wx/userInfo.udo")
    public String userInfo(HttpServletRequest request, HttpServletResponse response) {
        Users users = UserUtils.getUserInfo(request.getSession());
        Message message = new Message();

        if (users == null) {
            message.setStatus(-1);
        } else {
            if (UserUtils.getAuthority(request.getSession())) { // 管理
                message.setStatus(0);
            } else { // 用户
                message.setStatus(1);
            }
        }
        return JsonUtils.parseObject(message);
    }

    /**
     * @description 获取排名
     * @author 30500
     * @date 2021/7/1 17:34
     * @type [javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse]
     * @return java.lang.String
     */
    @ResponseText("/wx/getAllUser.udo")
    public String getAllRank(HttpServletRequest request, HttpServletResponse response) {
        List<Map<String, String>> result = service.getAllRank();
        Message message = new Message();

        if (result.size() == 0) {
            message.setStatus(-1);
            message.setResult("没有结果");
        } else {
            message.setStatus(0);
            message.setResult("查询成功");
            message.setData(result);
        }
        return JsonUtils.parseObject(message);
    }

    /**
     * @description 获取当前用户名
     * @author 30500
     * @date 2021/7/1 17:35
     * @type [javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse]
     * @return java.lang.String
     */
    @ResponseText("/wx/getUserName.udo")
    public String getUserName(HttpServletRequest request, HttpServletResponse response) {
        String userName = UserUtils.getUserInfo(request.getSession()).getUserName();
        Message message = new Message();
        if (userName != null) {
            message.setStatus(0);
            message.setData(userName);
        } else {
            message.setStatus(-1);
        }
        return JsonUtils.parseObject(message);
    }

    /**
     * @description 更新当前用户的信息
     * @author 30500
     * @date 2021/10/2 16:21
     * @type [javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse]
     * @return java.lang.String
     */
    @ResponseText("/wx/registerUser.udo")
    public String updateUserById(HttpServletRequest request, HttpServletResponse response) {
        String phone = request.getParameter("phone");
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        String code = request.getParameter("code");
        String cellCode = UserUtils.getTempInfo(request.getSession(), phone);
        Message message = new Message();
        // 验证是否是获取验证码的手机
        if (cellCode == null) {
            message.setStatus(-1);
            message.setResult("手机号没有获取验证码");
        } else {
            // 验证验证码是否正确
            if (cellCode.equals(code)) {
                int update = service.update(name, password, phone, null, UserUtils.getUserInfo(request.getSession()).getId());
                if (update == 1) {
                    message.setStatus(0);
                    message.setResult("更新成功");
                    Users users = service.getByPhone(phone, 1);
                    UserUtils.setUserInfo(request.getSession(), users);
                } else {
                    message.setStatus(-1);
                    message.setResult("更新失败");
                }
            } else {
                message.setStatus(-1);
                message.setResult("验证码错误");
            }
        }
        return JsonUtils.parseObject(message);
    }

    /**
     * @description 检查手机号是否已经被注册
     * @author 30500
     * @date 2021/10/4 8:20
     * @type [java.lang.String 手机号,
     *        int 身份]
     * @return boolean
     */
    public boolean checkPhone(String phone) {
        Users user = service.getByPhone(phone);
        if (user == null)
            return false;
        else
            return true;
    }

    /**
     * @description 更新手机号时获取验证码
     * @author 30500
     * @date 2021/10/4 8:38
     * @type [javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse]
     * @return java.lang.String
     */
    @ResponseText("/wx/getUpdateCode.udo")
    public String getUpdateCode(HttpServletRequest request, HttpServletResponse response) {
        String phone = request.getParameter("phone");
        Message message = new Message();
        if (checkPhone(phone)) {
            message.setStatus(-1);
            message.setResult("该手机号已完成注册");
            return JsonUtils.parseObject(message);
        }
        // 手机号可以注册, 获取验证码并更新临时手机
        return getCode(request, response);
    }

    /**
     * @description 更新手机号和姓名
     * @author 30500
     * @date 2021/10/4 8:55
     * @type [javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse]
     * @return java.lang.String
     */
    @ResponseText("/wx/updateNamePhone.udo")
    public String updateNamePhone(HttpServletRequest request, HttpServletResponse response) {
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String code = request.getParameter("code");
        if (UserUtils.getTempInfo(request.getSession(), phone).equals(code)) {
            int namePhone = service.updateNamePhone(name, phone, UserUtils.getUserInfo(request.getSession()).getId());
            if (namePhone == 1) {
                // 更新成功重新拉取数据
                Users byPhone = service.getByPhone(phone);
                UserUtils.setUserInfo(request.getSession(), byPhone);
                return JsonUtils.parseObject(new Message(0));
            }
            else
                return JsonUtils.parseObject(new Message(-1, "更新失败"));
        } else {
            return JsonUtils.parseObject(new Message(-1, "电话和验证码手机号不一致"));
        }
    }

    /**
     * @description 更新用户名
     * @author 30500
     * @date 2021/10/4 9:28
     * @type [javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse]
     * @return java.lang.String
     */
    @ResponseText("/wx/updateName.udo")
    public String updateName(HttpServletRequest request, HttpServletResponse response) {
        String name = request.getParameter("name");
        int namePhone = service.updateName(name, UserUtils.getUserInfo(request.getSession()).getId());
        if (namePhone == 1)
            return JsonUtils.parseObject(new Message(0));
        return JsonUtils.parseObject(new Message(-1, "更新失败"));
    }
}
